import RemoteDAO from './remoteDAO'

const util = {}
/**
  *处理用户中心的游客身份也可以查看的接口
  * 12008301,12008302,12008305,12008310,12008312,12008315,12008316
  * @param param
  * @param bridgeParam
  */
util.setTouristParam = function (param, bridgeParam) {
  let nickname = localStorage.getItem('nickname')
  let usercode = localStorage.getItem('userCode')
  if (nickname === '' || nickname === null || nickname === undefined) {
    /* 游客状态 */
    bridgeParam.param = param
    bridgeParam.touristParameter = {'mobile': 'phone_no', 'password': 'stock_pwd', 'user_code': 'user_code'}
  } else {
    /* 登录状态 */
    param.user_code = usercode
    bridgeParam.param = param
    bridgeParam.localParameter = {'mobile': 'phone_no', 'password': 'stock_pwd'}
  }
  return bridgeParam
}

/**
  *如果本地缓存中有token，就放入传送的参数中
  * @param param
  */
util.setToken = function (param) {
  let token = localStorage.getItem('yyztoken')
  if (token !== '' && token !== null) {
    param.gmas_token = token
  }
  return param
}

/**
 * 获取当前userCode
 */
util.getUserCode = function () {
  return localStorage.getItem('userCode')
}

/**
 * 检查是否已登录
 * @return {Boolean} 是否已登录
 */
util.checkUserCode = function () {
  const userCode = localStorage.getItem('userCode')
  if (!userCode) {
    RemoteDAO.callNativeLogin().then(response => {
      console.info('登录成功', response)
      setTimeout(() => {
        window.location.reload()
      }, 300)
    })
    return false
  }
  return true
}

/**
 * 获得默认头像
 * @param  {String} userCode 用户ID
 * @return {String}           默认头像地址
 */
util.getDefaultUserHead = function (userCode) {
  if (userCode) {
    const defaultIndex = parseInt(userCode) % 8 + 1
    return 'https://dl.app.gtja.com/pictures/niutou/tou' + defaultIndex + '@2x.png'
  } else {
    return ''
  }
}

/**
 * 埋点
 * @param  {string} eventId 事件号
 * @return {void}
 */
util.buryPoint = function (eventId) {
  console.info('埋点事件:', eventId)
  const { TDAPP } = window
  const kv = { userCode: localStorage.getItem('userCode') || '' }
  TDAPP.onEvent(eventId, '', kv)
  TDAPP.send()
}

export default util
